package com.superman.service.impl;

import com.superman.dao.ICarDao;
import com.superman.dao.IRecordDao;
import com.superman.dao.impl.CarDaoImpl;
import com.superman.dao.impl.RecordDaoImpl;
import com.superman.entity.Car_Brand_Category;
import com.superman.entity.Record;
import com.superman.entity.Record_User_Car;
import com.superman.service.IRentAndReturn;
import com.superman.util.ConnectionFactory;
import com.superman.util.CurrentPayment;
import com.superman.util.CurrentTime;
import com.superman.util.GUID;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class RentAndReturnImpl implements IRentAndReturn {
    ICarDao carDao = new CarDaoImpl();
    IRecordDao recordDao = new RecordDaoImpl();

    /**
     * 生成一条租车记录，修改汽车状态为不可租
     * @param user_id 当前租车用户id
     * @param car_number 当前用户选择的车牌
     * @return 生成的租车记录
     */
    @Override
    public List<Record_User_Car> rentCar(String user_id, String car_number) {
        List<Record_User_Car> list = new ArrayList<>();//返回生成的一条租车记录
        Connection conn = null;
        Record record = null;
        try {
            conn = ConnectionFactory.getConnection();
            conn.setAutoCommit(false);
            Car_Brand_Category cbc = carDao.queryCarByCarIdNumber(car_number).get(0);//从当前已上架且可租用的汽车列表中查找并返回选择的汽车对象
            if(cbc != null){//判断选择的汽车是否可租
                //修改汽车status状态为1 不可租用
                int row1 = carDao.userUpdateCarStatus(cbc.getId(), 1, conn);

                //插入一条record 记录
                record = new Record(GUID.getGUID(), user_id, cbc.getId(), CurrentTime.getCurrentTime(), null, 0);
                int row2 = recordDao.insertRecord(record, conn);

            }
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            conn.rollback();
        } finally {
            try {
                list = recordDao.queryRecordByRecordID(record.getId());//返回新生成的租车记录
                ConnectionFactory.close(conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list;
        }

    }

    /**
     * 还车，修改租车记录，添加还车时间和租金总额
     * @param car_id 要归还的车辆
     * @return 修改后的租车记录
     */
    @Override
    public List<Record_User_Car> returnCar(String car_id) {
        List<Record_User_Car> list = new ArrayList<>();//返回还车记录
        Connection conn = null;
        Record_User_Car ruc = null;
        try {
            conn = ConnectionFactory.getConnection();
            conn.setAutoCommit(false);
            ruc = recordDao.queryRecordByCarIdAndNoReturnInfo(car_id).get(0);//返回当前车辆的未还车记录

            //为还车记录添加还车时间和租金总额
            String current_date = CurrentTime.getCurrentTime();
            double payment = CurrentPayment.getCurrentPayment(ruc.getStart_date(), current_date, ruc.getRent());
            int row1 = recordDao.updateRecord(car_id, current_date, payment, conn);
            //修改汽车状态为可租用
            int row2 = carDao.userUpdateCarStatus(car_id, 0, conn);
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            conn.rollback();
        } finally {
            try {
                list = recordDao.queryRecordByRecordID(ruc.getId());//返回当前还车记录
                ConnectionFactory.close(conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list;
        }

    }
}
